﻿var _{{id}} = (function() {
    return {
        update: function(payload) {
            var host = $('#{{id}}');
            var canvas;

            if (host.children('canvas').length === 0) {
                // remove placeholder & add our canvas element
                host.children().remove();
                canvas = $(document.createElement('canvas'))
                    .attr('width', host.parent().width())
                    .attr('height', host.parent().height());
                host.append(canvas);                
            } else {
                canvas = host.children('canvas');
            }

            var ctx = canvas.get(0).getContext('2d');

            var jsonPayload = JSON.parse(payload);
            new Chart(ctx).Bar(jsonPayload.data, jsonPayload.options);
        }
    }
}());
